package dcc.resources.code;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Zip_UnZip {

	private static void unZip(String des, String zipFileAllName) {
		ZipFile zipFile = null;
		InputStream is = null;
		FileOutputStream fos = null;
		try {
			zipFile = new ZipFile(zipFileAllName);
			Enumeration<?> enu = zipFile.entries();
			while (enu.hasMoreElements()) {
				ZipEntry zipEntry = (ZipEntry) enu.nextElement();
				String name = des + zipEntry.getName();
				File file = new File(name);
				File parent = file.getParentFile();
				if (parent != null) {
					parent.mkdirs();
				}
				is = zipFile.getInputStream(zipEntry);
				fos = new FileOutputStream(file);
				byte[] bytes = new byte[1024];
				int length;
				while ((length = is.read(bytes)) >= 0) {
					fos.write(bytes, 0, length);
				}
				is.close();
				fos.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (zipFile != null) {
					zipFile.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		String zippedFile = "C:\\测试\\Test.zip";
		String zipDes = "C:\\测试\\unZip\\";
		try {
			unZip(zipDes, zippedFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
